//
//  DrawerViewController.m
//  Project_ZFR_MUSIC
//
//  Created by 张福润 on 16/2/24.
//  Copyright © 2016年 张福润. All rights reserved.
//

#import "DrawerViewController.h"

@interface DrawerViewController ()<UIGestureRecognizerDelegate>

@end

@implementation DrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    
    if(!self.tap){
        self.tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        self.tap.delegate=self;
        self.tap.enabled=NO;
        [self.view addGestureRecognizer:self.tap];
        
    }
    // Do any additional setup after loading the view.
}

//-(UIViewController *)rootVC{
//    if(!_rootVC){
//        _rootVC=[[UIViewController alloc]init];
//
//    }
//    return _rootVC;
//}
-(void)tapAction:(id)sender{
    [self.tap setEnabled:NO];
    [self showRootViewController:nil];
}
//设置根视图控制器
-(void)setRootViewController:(UIViewController *)viewController{
    //如果传进来是空的,直接返回
    if(!viewController){
        
        return;
    }
    //设置中间视图的控制器
    [self.rootVC.view removeFromSuperview];
    self.rootVC =viewController;
    [self addChildViewController:self.rootVC];
    [self.view addSubview:self.rootVC.view];

    if(self.tap.enabled){
        self.rootVC.view.frame=CGRectMake(375*0.6, 667*0.05, 375, 667*0.9);
    }
    //关闭抽屉
    [self showRootViewController:nil];
    //设置导航栏
    [self setNaButton];
    
    
}
//展示中间视图的方法(关闭抽屉)
-(void)showRootViewController:(UIViewController *)viewController{
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame=CGRectMake(0, 0, 375, 667);
    }];
    self.tap.enabled=NO;
    self.rootVC.view.userInteractionEnabled=YES;
}


//设置左上角
-(void)setNaButton{
    UINavigationController *na =(UINavigationController *)self.rootVC;
    UIViewController *vc=na.viewControllers[0];
    vc.navigationItem.leftBarButtonItem=({
        UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithTitle:@"抽屉" style:(UIBarButtonItemStyleDone) target:self action:@selector(openMenu)];
        item;
    });
}
//打开抽屉事件
-(void)openMenu{
    [self showLeftViewController:nil];
}

//打开抽屉
-(void)showLeftViewController:(UIViewController *)leftViewController{
    if(![self.view.subviews containsObject:self.leftVC.view]){
        //插入抽屉的view
        [self.view insertSubview:self.leftVC.view atIndex:0];
    }
    //动画移动根视图控制器
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame=CGRectMake(375*0.6, 667*0.05, 375, 667*0.9);
    }];

    //打开手势
    self.tap.enabled=YES;
    
    self.rootVC.view.userInteractionEnabled=NO;
}
//手势事件
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if(gestureRecognizer == self.tap){
        return CGRectContainsPoint(self.view.frame, [gestureRecognizer locationInView:self.rootVC.view]);
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
